// pages/mine/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        username: "",
        userage: "",
        pic: "", //头像
        address: "",
        location: "", //选择位置
        db: wx.cloud.database(),
        list: [], //用户列表
        num: 10

    },
    delById(e) {
        let id = e.detail.id; //删除的编号
        // this.data.db.collection("userinfo").doc(id).remove().then(res => {
        //     this.setData({
        //         list: this.data.list.filter(item => item._id != id),
        //     })
        // })
        wx.cloud.callFunction({
            name: "userinfo",
            data: {
                type: 'del',
                id
            }
        }).then(res => {
            this.setData({
                list: this.data.list.filter(item => item._id != id),
            })
        })
    },
    addNum() {
        this.setData({
            num: this.data.num + 1
        })
    },
    setUsername(e) { //用户名赋值
        this.setData({
            username: e.detail.value
        })
    },
    setUserage(e) { //年龄赋值
        this.setData({
            userage: e.detail.value
        })
    },
    chooseImg() { //选择图片
        wx.chooseMedia({
            count: 1,
            success: (res) => {
                this.setData({
                    pic: res.tempFiles[0]["tempFilePath"]
                })
                console.log(res);
            }
        })
    },
    chooseAddress() { //选择地址
        wx.chooseAddress().then(res => {
            this.setData({
                address: res.detailInfo
            })
        })
    },
    chooseLocation() { //选择位置
        wx.chooseLocation().then(res => {
            this.setData({
                location: res.address
            })
        })
    },
    submit() { //提交获取内容

        console.log(this.data.username + "和" + this.data.userage + "头像" + this.data.pic);

        wx.cloud.uploadFile({
            cloudPath: 'userinfo/' + Date.now() + ".jpg", // 上传至云端的路径
            filePath: this.data.pic, // 小程序临时文件路径
            success: res2 => {
                // 返回文件 ID

                console.log("图片上传地址为:" + res2.fileID)
                wx.cloud.callFunction({
                    name: "userinfo",
                    data: {
                        type: "add",
                        username: this.data.username,
                        userage: this.data.userage,
                        pic: res2.fileID
                    }
                }).then(res => {
                    //console.log(res);
                    this.setData({
                        list: [...this.data.list, {
                            username: this.data.username,
                            userage: this.data.userage,
                            pic: res2.fileID,
                            _id: res.result._id
                        }]
                    })
                })

                // this.data.db.collection("userinfo").add({
                //     data: {
                //         username: this.data.username,
                //         userage: this.data.userage,
                //         pic: res2.fileID
                //     },
                //     success: (res) => {
                //         console.log(res);
                //         this.setData({
                //             list: [...this.data.list, {
                //                 username: this.data.username,
                //                 userage: this.data.userage,
                //                 pic: res2.fileID,
                //                 _id: res._id
                //             }]
                //         })
                //     },
                //     fail: (res) => {
                //         console.log(res);
                //     }

                // })
            },
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // this.data.db.collection("userinfo").get({
        //     success: (res) => {
        //         console.log(res);
        //         this.setData({ //页面加载 初始化集合
        //             list: res.data
        //         })
        //     }
        // })
        wx.cloud.callFunction({
            name: "userinfo"
        }).then(res => {
            this.setData({
                list: res.result.data
            })
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})